<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Home\Controller;
use Think\Controller;

/**
 * 前台公共控制器
 * 为防止多分组Controller名称冲突，公共Controller名称统一使用分组名称
 */
class HomeController extends Controller {

	/* 空操作，用于输出404页面 */
	public function _empty(){
		$this->redirect('Index/index');
	}
    protected function _initialize(){

        include('jssdnController.class.php');
        session("late_url",get_url());
        /* 读取站点配置 */
        $jssdk = new JSSDK("wxaffe6056dab62b11", "af5a8797a8041187d5407c1630e62d53");
        $signPackage = $jssdk->GetSignPackage();
        $this->assign('signPackage',$signPackage);
        $this->assign("uid",session("uid"));
        if (!session("share_sale_content2")){
            session("share_sale_content2",M('salesoutlets')->where(array('id'=>session("salesoutlets_id")))->field('sales_number,sales_name')->find());
        }
        $this->assign("share_sale_content",session("share_sale_content2"));
        $this->assign("share_id",think_encrypt(session('salesoutlets_id'),'share'));
//        $this->assign("cat_num",$cat_num);
    }
    private function get_share_id(){
        $sale=I("get.share_id",'');
        if($sale){
            $sale_id=think_decrypt($sale,'share');
            $stype=M("salesoutlets")->where(array('id'=>$sale_id))->getField('sales_stype');
            if ($stype){
                session("salesoutlets_id",$sale_id);
            }else{
                session_unset();
                $this->redirect('SalesChose/select_addr');
                //$this->error("网点错误");

                exit();
            }
        }else{
            if (!session("salesoutlets_id")){
                session_unset();
                $this->redirect('SalesChose/select_addr');
               // $this->error("网点错误");
                exit();
            }
        }
        if(session("salesoutlets_id")){
            $this->checkStatus();
        }
    }

    public function checkStatus(){
//        session("uid",18);
//        session('weixinid',18);
        if (session("uid")){
           return true;
           }
                header('Location:https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxaffe6056dab62b11&redirect_uri=http%3A%2F%2Fwww.hmgx8.com/index.php/Home/Weixin/weixinLogin&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect');
               exit();
    }
    /*
  * 功能:
  *      向服务器发送Get请求
  * 参数:
  *      $url    请求地址
  *      $protocolType   1:http 2:https
  * 返回值:
  *      返回服务器的返回信息
  */

    private static function SendGetRequest($url, $protocolType = 1, $jumpRedirect = true)
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

        if ($protocolType == 2) {
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        }

        $data = curl_exec($curl);
        $responseInfo = curl_getinfo($curl);

        if ($responseInfo['redirect_url'] && $jumpRedirect) {
            return self::SendGetRequest($responseInfo['redirect_url']);
        }

        curl_close($curl);

        return $data;
    }


}
